var selectedRow='';
var isInit = true;

$(function () {

    initTextSearch("#isvName",
        basePath + 'checkBizDocApp/getIsvData?t='+new Date().getTime()+'&isvName=',
        ['isvPid', 'isvName'],
        {'isvPid':'服务商PID', 'isvName':'服务商名称'},
        'isvPid',
        'isvName',
        true,
        selectIsvValue,
        noSelectIsvValue,
        false);
    //事件
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openEditPage('info');
        },
        'click .mod': function(e, value, row, index) {
            //编辑操作
            selectedRow=row;
            openEditPage('edit');
        },
        'click .delete' : function(e, value, row, index) {
            //删除操作
            selectedRow=row;
            deleteRole();
        },
        'click .statusSet': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            changeState();
        }
    }
});

function selectIsvValue(data) {
    if (!data || !data.isvPid) {
        return;
    }
    $('#isvPid').val(data.isvPid);
}

function noSelectIsvValue() {
    $('#isvPid').val('');
}

function formatStatus(value, row, index){
    return convertMapCodeText(statusEnumMap,value);
}
function formatCellEllipsis(value, row, index) {
    return {
        css: {
            "overflow": "hidden",
            "text-overflow":"ellipsis",
            "white-space": "nowrap"
        }
    }
}

function getParams(params) {
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        pageSize = searchInfo.pageSize ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo.pageNo ? searchInfo.pageNo : params.pageNumber;
    }
    var postData = {
        pageSize : pageSize,
        pageNo : pageNo
    };
    postData.roleName = $.trim($("#roleName").val());
     postData.status = $.trim($("#status").val());
     if($("#isvPid")){
         postData.isvPid = $.trim($("#isvPid").val());
     }
     return postData;
}
function openEditPage(command) {
    console.log(111)
    if (command != 'add' && command != 'edit'&&command != 'info') return;
    var id="";
    if (command != 'add') {
        if (!selectedRow || selectedRow.id == '') {
            toastr.error(common_no_choose_msg.format("操作"));
            return;
        }
        id=selectedRow.id;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + "pfRole/editPfRolePage?type=" + command + "&id=" + id + '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}
function doRefresh() {
    selectedRow = '';
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'pfRole/getGridData?t='+new Date().getTime()
    });
}
function changeState() {
    if (!selectedRow.status) {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var status = 'disabled';
    var oper = "作废";
    if (selectedRow.status == 'disabled') {
        status = 'enabled';
        oper = "恢复";
    }else{
        if (selectedRow.id==1) {
            toastr.error('系统默认角色不允许作废！');
            return;
        }
    }
    var postData = {
        id: selectedRow.id,
        status: status
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pfRole/changeState',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success: function (data) {
            if (data && data.isSuccess) {
                toastr.success('数据' + oper + '成功');
                doRefresh();
            } else if (data && !data.isSuccess) {
                toastr.error(data.errorDesc?data.errorDesc:'数据'+oper+'失败');
            }
        },
        error: function (request) {
            toastr.error('数据'+oper+'异常');
        }
    });
}
    function deleteRole() {
        if (!selectedRow.id) {
            toastr.error(common_no_choose_msg.format("操作"));
            return;
        }
        if (selectedRow.id==1) {
            toastr.error('系统默认角色不允许删除！');
            return;
        }
        swal({
                title:"",
                text:'[' + selectedRow.roleName + ']当前处于[' + convertMapCodeText(statusEnumMap,selectedRow.status)+ ']状态，确认删除？',
                type:"warning",
                showCancelButton:"true",
                showConfirmButton:"true",
                confirmButtonText:"确定",
                cancelButtonText:"取消",
                animation:"slide-from-top"
            }, function() {
                var postData = {
                    id: selectedRow.id,
                    status: 'delete'
                };
                $.ajax({
                    type: 'post',
                    url: basePath + 'pfRole/changeState',
                    traditional: true,
                    dataType: 'json',
                    contentType: "application/json",
                    data: JSON.stringify(postData)
                }).done(function(data) {
                    if (data && data.isSuccess) {
                        toastr.success('数据删除成功！');
                        doRefresh();
                    } else if (data && !data.isSuccess) {
                        toastr.error(data.errorDesc?data.errorDesc:'数据删除失败');
                    }
                }).error(function(data) {
                    toastr.error(common_system_error);
                });
            });
    }
// 单机选中行
$('#dataTable').on("click-row.bs.table", function (e, row, $element) {
    $('#dataTable .success').removeClass('success');//去除之前选中的行的，选中样式
    $element.addClass('success');//添加当前选中的 success样式用于区别
// 按钮添加 class 为 change_text
    changeStatueBtn(row.status == 'enabled',"冻结","恢复");
    selectedRow = row;
});

function openSetBtnPage() {
    if (!selectedRow || selectedRow == '') {
        toastr.error('请选择一行后再进行操作');
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + "pfRole/setBtnPage?id=" + selectedRow.id + '&' + $('#searchForm').serialize() +
        '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function openSetTabPage() {
    if (!selectedRow || selectedRow == '') {
        toastr.error('请选择一行后再进行操作');
        return;
    }
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + "pfRole/setTabPage?id=" + selectedRow.id + '&' + $('#searchForm').serialize() +
        '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}
